﻿package com.gengine.core.frame
{
    import com.gengine.core.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class FlashFrame extends Frame implements IFlashFrame
    {
        private var _sprite:Sprite;
        private var _isBrowserAvailable:Boolean = false;
        private var _renderCount:int = 0;
        private var _accruedTime:Number;
        private var _startTime:Number;
        private var _currentTime:int;
        private var _frameMap60:Dictionary;
        private var _prevTime:int = 0;
        private var _prevTheoryTime:Number = 0;
        protected var _rendererCount:int = 0;
        protected var lastInterval:int = 0;
        public static var AverageTime:int = 16;
        private static const FrameTheoryInterval:Number = 16.6667;

        public function FlashFrame(param1:Number = 24)
        {
            super(param1);
            this._frameMap60 = new Dictionary();
            this._sprite = new Sprite();
            return;
        }// end function

        override public function play() : void
        {
            super.play();
            this._sprite.addEventListener(Event.ENTER_FRAME, this.onEnterFrameHandler);
            this._renderCount = 0;
            this._startTime = getTimer();
            return;
        }// end function

        override public function stop() : void
        {
            super.stop();
            this._sprite.removeEventListener(Event.ENTER_FRAME, this.onEnterFrameHandler);
            return;
        }// end function

        public function addTimer60(param1:IBaseTimer) : void
        {
            if (!_isPlay)
            {
                this.play();
            }
            if (param1.delay == 1)
            {
                this._frameMap60[param1.id] = param1;
                var _loc_3:* = _count + 1;
                _count = _loc_3;
            }
            return;
        }// end function

        public function removeTimer60(param1:IBaseTimer) : void
        {
            if (this._frameMap60[param1.id])
            {
                delete this._frameMap60[param1.id];
                var _loc_3:* = _count - 1;
                _count = _loc_3;
            }
            return;
        }// end function

        private function recoupFrames(param1:uint = 1, param2:Number = 17) : void
        {
            var _loc_3:Boolean = true;
            var _loc_4:int = 0;
            while (_loc_4 < param1)
            {
                
                if (_loc_4 == (param1 - 1))
                {
                    _loc_3 = false;
                }
                this.renderer(param2, _loc_3);
                _loc_4++;
            }
            return;
        }// end function

        override protected function renderer(param1:int, param2:Boolean = false) : void
        {
            var _loc_3:IBaseTimer = null;
            var _loc_4:String = this;
            var _loc_5:* = this._rendererCount + 1;
            _loc_4._rendererCount = _loc_5;
            this._prevTheoryTime = this._prevTheoryTime + FrameTheoryInterval;
            for each (_loc_3 in this._frameMap60)
            {
                
                if (_loc_3.running)
                {
                    _loc_3.interval = param1;
                    _loc_3.renderer(this._rendererCount, param2);
                    continue;
                }
                this.removeTimer60(_loc_3);
            }
            if (this._rendererCount % 2)
            {
                this.lastInterval = param1;
            }
            else
            {
                super.renderer(this.lastInterval + param1);
            }
            return;
        }// end function

        public function getDebugMsg() : String
        {
            var _loc_2:int = 0;
            var _loc_3:IBaseTimer = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_1:String = "";
            _loc_1 = _loc_1 + ("----------------------\n_rendererCount:" + this._rendererCount);
            _loc_1 = _loc_1 + ("\n_frameCount:" + _frameCount);
            for each (_loc_3 in this._frameMap60)
            {
                
                _loc_2++;
            }
            _loc_1 = _loc_1 + ("\nframe60Len:" + _loc_2);
            for each (_loc_3 in _oneFrameMap)
            {
                
                _loc_4++;
            }
            _loc_1 = _loc_1 + ("\nframe30OneMapLen:" + _loc_4);
            for each (_loc_3 in _otherFrameMap)
            {
                
                _loc_5++;
            }
            _loc_1 = _loc_1 + ("\nframe30OtherMapLen:" + _loc_5);
            _loc_1 = _loc_1 + ("\n_count:" + _count);
            return _loc_1;
        }// end function

        public function getTime() : int
        {
            return this._currentTime;
        }// end function

        override protected function onEnterFrameHandler(event:Event = null) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_2:* = getTimer();
            if (int(_loc_2 / 1000) - int(this._currentTime / 1000) >= 1)
            {
                FrameManager.isSecUpdate = true;
            }
            this._currentTime = _loc_2;
            FrameUtil.frameStartTime = _loc_2;
            if (this._prevTime == 0)
            {
                _loc_3 = 0;
                this._prevTheoryTime = _loc_2;
            }
            else
            {
                _loc_3 = _loc_2 - this._prevTime;
                _loc_4 = (_loc_2 - this._prevTheoryTime) / FrameTheoryInterval;
                if (_loc_4 < 2)
                {
                    this.renderer(_loc_3);
                }
                else
                {
                    this.recoupFrames(_loc_4, _loc_3 / _loc_4);
                }
            }
            this._prevTime = _loc_2;
            MapFrameUtil.render();
            var _loc_6:String = this;
            var _loc_7:* = this._renderCount + 1;
            _loc_6._renderCount = _loc_7;
            if (this._renderCount == 50)
            {
                _loc_5 = getTimer();
                AverageTime = (_loc_5 - this._startTime) / this._renderCount;
                this._renderCount = 0;
                this._startTime = _loc_5;
            }
            return;
        }// end function

        public function get rendererCount() : int
        {
            return this._rendererCount;
        }// end function

    }
}
